﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace UniRx
{
	// Token: 0x020003DB RID: 987
	public class ObservableYieldInstruction<T> : IEnumerator<T>, ICustomYieldInstructionErrorHandler, IEnumerator, IDisposable
	{
		// Token: 0x0600142B RID: 5163 RVA: 0x0005A17C File Offset: 0x0005857C
		public ObservableYieldInstruction(IObservable<T> source, bool reThrowOnError, CancellationToken cancel)
		{
			this.moveNext = true;
			this.reThrowOnError = reThrowOnError;
			this.cancel = cancel;
			try
			{
				this.subscription = source.Subscribe(new ObservableYieldInstruction<T>.ToYieldInstruction(this));
			}
			catch
			{
				this.moveNext = false;
				throw;
			}
		}

		// Token: 0x17000177 RID: 375
		// (get) Token: 0x0600142C RID: 5164 RVA: 0x0005A1D8 File Offset: 0x000585D8
		public bool HasError
		{
			get
			{
				return this.error != null;
			}
		}

		// Token: 0x17000178 RID: 376
		// (get) Token: 0x0600142D RID: 5165 RVA: 0x0005A1E6 File Offset: 0x000585E6
		public bool HasResult
		{
			get
			{
				return this.hasResult;
			}
		}

		// Token: 0x17000179 RID: 377
		// (get) Token: 0x0600142E RID: 5166 RVA: 0x0005A1F0 File Offset: 0x000585F0
		public bool IsCanceled
		{
			get
			{
				return !this.hasResult && this.error == null && this.cancel.IsCancellationRequested;
			}
		}

		// Token: 0x1700017A RID: 378
		// (get) Token: 0x0600142F RID: 5167 RVA: 0x0005A228 File Offset: 0x00058628
		public bool IsDone
		{
			get
			{
				return this.HasResult || this.HasError || this.cancel.IsCancellationRequested;
			}
		}

		// Token: 0x1700017B RID: 379
		// (get) Token: 0x06001430 RID: 5168 RVA: 0x0005A25C File Offset: 0x0005865C
		public T Result
		{
			get
			{
				return this.result;
			}
		}

		// Token: 0x17000174 RID: 372
		// (get) Token: 0x06001431 RID: 5169 RVA: 0x0005A264 File Offset: 0x00058664
		T IEnumerator<T>.Current
		{
			get
			{
				return this.current;
			}
		}

		// Token: 0x17000175 RID: 373
		// (get) Token: 0x06001432 RID: 5170 RVA: 0x0005A26C File Offset: 0x0005866C
		object IEnumerator.Current
		{
			get
			{
				return this.current;
			}
		}

		// Token: 0x1700017C RID: 380
		// (get) Token: 0x06001433 RID: 5171 RVA: 0x0005A279 File Offset: 0x00058679
		public Exception Error
		{
			get
			{
				return this.error;
			}
		}

		// Token: 0x06001434 RID: 5172 RVA: 0x0005A284 File Offset: 0x00058684
		bool IEnumerator.MoveNext()
		{
			if (!this.moveNext)
			{
				if (this.reThrowOnError && this.HasError)
				{
					throw this.Error;
				}
				return false;
			}
			else
			{
				if (this.cancel.IsCancellationRequested)
				{
					this.subscription.Dispose();
					return false;
				}
				return true;
			}
		}

		// Token: 0x17000176 RID: 374
		// (get) Token: 0x06001435 RID: 5173 RVA: 0x0005A2DC File Offset: 0x000586DC
		bool ICustomYieldInstructionErrorHandler.IsReThrowOnError
		{
			get
			{
				return this.reThrowOnError;
			}
		}

		// Token: 0x06001436 RID: 5174 RVA: 0x0005A2E4 File Offset: 0x000586E4
		void ICustomYieldInstructionErrorHandler.ForceDisableRethrowOnError()
		{
			this.reThrowOnError = false;
		}

		// Token: 0x06001437 RID: 5175 RVA: 0x0005A2ED File Offset: 0x000586ED
		void ICustomYieldInstructionErrorHandler.ForceEnableRethrowOnError()
		{
			this.reThrowOnError = true;
		}

		// Token: 0x06001438 RID: 5176 RVA: 0x0005A2F6 File Offset: 0x000586F6
		public void Dispose()
		{
			this.subscription.Dispose();
		}

		// Token: 0x06001439 RID: 5177 RVA: 0x0005A303 File Offset: 0x00058703
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}

		// Token: 0x04000BB0 RID: 2992
		private readonly IDisposable subscription;

		// Token: 0x04000BB1 RID: 2993
		private readonly CancellationToken cancel;

		// Token: 0x04000BB2 RID: 2994
		private bool reThrowOnError;

		// Token: 0x04000BB3 RID: 2995
		private T current;

		// Token: 0x04000BB4 RID: 2996
		private T result;

		// Token: 0x04000BB5 RID: 2997
		private bool moveNext;

		// Token: 0x04000BB6 RID: 2998
		private bool hasResult;

		// Token: 0x04000BB7 RID: 2999
		private Exception error;

		// Token: 0x020003DC RID: 988
		private class ToYieldInstruction : IObserver<T>
		{
			// Token: 0x0600143A RID: 5178 RVA: 0x0005A30A File Offset: 0x0005870A
			public ToYieldInstruction(ObservableYieldInstruction<T> parent)
			{
				this.parent = parent;
			}

			// Token: 0x0600143B RID: 5179 RVA: 0x0005A319 File Offset: 0x00058719
			public void OnNext(T value)
			{
				this.parent.current = value;
			}

			// Token: 0x0600143C RID: 5180 RVA: 0x0005A327 File Offset: 0x00058727
			public void OnError(Exception error)
			{
				this.parent.moveNext = false;
				this.parent.error = error;
			}

			// Token: 0x0600143D RID: 5181 RVA: 0x0005A341 File Offset: 0x00058741
			public void OnCompleted()
			{
				this.parent.moveNext = false;
				this.parent.hasResult = true;
				this.parent.result = this.parent.current;
			}

			// Token: 0x04000BB8 RID: 3000
			private readonly ObservableYieldInstruction<T> parent;
		}
	}
}
